/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.generator.apiGenerator.CefApiGeneratorContext;
import com.inspur.edp.cef.generator.coreGenerator.base.mgrGenerator.ManagerGeneratorContext;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefModelResInfoImpl;
import com.inspur.edp.cef.spi.entity.resourceInfo.builinImpls.CefValueObjModelResInfo;
import com.inspur.edp.das.commonmodel.IGspCommonObject;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;

public class GetModelResInfoGenerator extends ClassMethodGenerator {

    public static final String MethodName = "getModelResourceInfo";
    protected String fieldName;
    protected ManagerGeneratorContext parentContext;


    public GetModelResInfoGenerator(String fieldName, ManagerGeneratorContext parentContext) {
        this.fieldName = fieldName;
        this.parentContext = parentContext;
    }

    @Override
    protected String getMethodName() {
        return MethodName;
    }

    @Override
    protected TypeInfo getReturnType() {
        return parentContext.getProjContext().getEntityGeneratorContext().getNode() instanceof IGspCommonObject ?
                new TypeInfo(CefModelResInfoImpl.class) : new TypeInfo(CefValueObjModelResInfo.class);
    }

    @Override
    protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
        ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
        list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
        list.add(Modifier.ModifierKeyword.STATIC_KEYWORD);
        return list;
    }

    @Override
    protected ArrayList<ParameterInfo> getParameterCollection() {
        return new ArrayList<>();
    }

    @Override
    protected Block buildMethodBody() {
        Block block = ast.newBlock();
        IfStatement ifStatement = ast.newIfStatement();
        buildIfStatement(ifStatement);
        buildThenStatement(ifStatement);
        block.statements().add(ifStatement);

        block.statements().add(buildReturnStatement());

        return block;
    }

    private void buildIfStatement(IfStatement ifStatement) {
        InfixExpression inifExpression = ast.newInfixExpression();
        inifExpression.setLeftOperand(ast.newSimpleName(fieldName));
        inifExpression.setOperator(InfixExpression.Operator.EQUALS);
        inifExpression.setRightOperand(ast.newNullLiteral());
        ifStatement.setExpression(inifExpression);
    }

    protected void buildThenStatement(IfStatement ifStatement) {

        Assignment assignment = ast.newAssignment();
        assignment.setOperator(Assignment.Operator.ASSIGN);
        assignment.setLeftHandSide(ast.newSimpleName(fieldName));
        ClassInstanceCreation creation = ast.newClassInstanceCreation();
        String typeName = ((CefApiGeneratorContext) parentContext.getProjContext().getCefGenContext()
                .getApiGenContext()).getMgrInfoType().getFullName();
        TypeInfo type = new TypeInfo(typeName);
        creation.setType(type.getType(ast, parentContext.getProjContext()));
        assignment.setRightHandSide(creation);
        ExpressionStatement expressionStatement = ast.newExpressionStatement(assignment);

        ifStatement.setThenStatement(expressionStatement);
    }

    private ReturnStatement buildReturnStatement() {
        ReturnStatement statement = ast.newReturnStatement();
        statement.setExpression(ast.newSimpleName(fieldName));
        return statement;
    }

}
